<script>
export default {
  name: "userRole",
  data() {
    return {
      userData: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      Authorities: localStorage.getItem("authorities") ? JSON.parse(localStorage.getItem("authorities")) : {},
      tableData: [],
      userID: '',
      username: "",
      roleID: '',
      roleCode: '',
      roleName: "",
      userRole: {},
      dialogFormVisible: false,
      userIDVisible: false,
      deleted_id: 0,
      EditDisabled: false,
      DeleteDisabled: false,
    }
  },
  created(){
    this.firstLoad();
    this.EditDisabled = this.Authorities.Authority10;
    this.DeleteDisabled = this.Authorities.Authority9;
    console.log(this.Authorities);
  },
  methods: {
    firstLoad() {
      this.request.get("http://localhost:9090/user/allRole").then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    load() {
      this.request.get("http://localhost:9090/user/findRole",  {
        params: {
          userID: (this.userID === '' ? 0 : this.userID),
          username: this.username,
          roleID: (this.roleID === '' ? 0 : this.roleID),
          roleCode: (this.roleCode === '' ? 0 :this.roleCode),
          roleName: this.roleName
        }
      }).then(res => {
        console.log(res);
        this.tableData = res.data;
        this.total = res.total;
      })
    },
    saveUserRole() {
      this.request.post("http://localhost:9090/user/saveUserRole", this.userRole).then(res => {
        if (res) {
          this.$message.success("保存成功");
          this.dialogFormVisible = false;
          this.firstLoad();
        } else {
          this.$message.error("保存失败");
        }
      })
    },
    handleEdit(row) {
      this.userRole = row;
      this.userRole.preUserID = row.userID;
      this.userRole.userID = row.userID;
      this.userRole.preRoleID = row.roleID;
      this.userRole.roleID = row.roleID;
      console.log(this.userRole);
      this.userIDVisible = false;
      this.dialogFormVisible = true;
    },
    reset(){
      this.userID = '';
      this.username = "";
      this.roleID = '';
      this.roleCode = '';
      this.roleName = "";
      this.firstLoad();
    },
    handleAdd() {
      this.dialogFormVisible = true;
      this.userIDVisible = true;
      this.userRole = {};
    },
    handleDel(row) {
      this.request.delete("http://localhost:9090/user/deleteUserRole" , {
        params: {
          userID: row.userID,
          roleID: row.roleID
        }
      }).then(res => {
        if (res) {
          this.$message.success("删除成功");
          this.firstLoad();
        } else {
          this.$message.error("删除失败");
        }
      })
    }
  }
}
</script>

<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 150px" placeholder="请输入用户ID" suffix-icon="el-icon-search" v-model="userID" :disabled="!this.Authorities.Authority11"></el-input>
      <el-input style="width: 150px" placeholder="请输入用户名" suffix-icon="el-icon-search" class="ml-5" v-model="username" :disabled="!this.Authorities.Authority11"></el-input>
      <el-input style="width: 150px" placeholder="请输入角色ID" suffix-icon="el-icon-message" class="ml-5" v-model="roleID" :disabled="!this.Authorities.Authority11"></el-input>
      <el-input style="width: 150px" placeholder="请输入角色编码" suffix-icon="el-icon-message" class="ml-5" v-model="roleCode" :disabled="!this.Authorities.Authority11"></el-input>
      <el-input style="width: 150px" placeholder="请输入角色名" suffix-icon="el-icon-position" class="ml-5" v-model="roleName" :disabled="!this.Authorities.Authority11"></el-input>
      <el-button class="ml-5" type="primary" @click="load" :disabled="!this.Authorities.Authority11">搜索</el-button>
      <el-button type="warning" @click="reset" :disabled="!this.Authorities.Authority11">重置</el-button>
      <el-button class=“ml-5” type="primary" @click="handleAdd" :disabled="!this.Authorities.Authority8">新增<i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg">
      <el-table-column prop="userID" label="userID" width="130"></el-table-column>
      <el-table-column prop="username" label="用户名" width="160"></el-table-column>
      <el-table-column prop="roleID" label="roleID" width="160"></el-table-column>
      <el-table-column prop="roleCode" label="角色编码" width="160"></el-table-column>
      <el-table-column prop="roleName" label="角色名" width="160"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" :disabled="!EditDisabled">编辑<i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              title="确定删除这条数据吗？"
              icon="el-icon-info"
              icon-color="red"
              confirm-button-text="确定"
              cancel-button-text="取消"
              @confirm="handleDel(scope.row)"
          >
            <el-button type="danger" slot="reference"  :disabled="!DeleteDisabled">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px">
        <el-form-item label="原用户ID" v-show="false">
          <el-input v-model="userRole.preUserID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户ID" v-show="userIDVisible">
          <el-input v-model="userRole.userID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="原角色ID" v-show="false">
          <el-input v-model="userRole.preRoleID" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色ID">
          <el-input v-model="userRole.roleID" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUserRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>